package cn.kgc.config;

import cn.kgc.constant.OrderConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>添加描述信息</p>
 *
 * @author 卢传琪
 */
@Configuration
public class RabbitConfig {
    @Bean
    public Exchange orderExchange(){
        return new TopicExchange(OrderConstant.ORDER_EXCHANGE,true,false);
    }

    @Bean
    public Queue orderDelayQueue(){
        Map<String, Object> arguments=new HashMap<>();
        //设置队列的过期时间
        arguments.put("x-message-ttl",60000);
        //设置死信交换机
        arguments.put("x-dead-letter-exchange",OrderConstant.ORDER_EXCHANGE);
        //设置死信路由键
        arguments.put("x-dead-letter-routing-key","order.release");

        Queue queue= new Queue("order.delay.queue",true,true,false,arguments);
        return queue;
    }

    @Bean
    public  Queue orderReleaseQueue(){
        return new Queue(OrderConstant.ORDER_RELEASE_QUEUE,true,true,false);
    }

    @Bean
    public Binding orderDelayQueueBinding(){
        //如果向交换机中发送一个信息，如果rountingKey是order.create.order
        //此信息就会进入到order.delay.queue队列中
        return new Binding("order.delay.queue",Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EXCHANGE,OrderConstant.CREATE_ORDER_ROUTING_KEY,null);
    }

    @Bean
    public Binding orderReleaseQueueBinding(){
        //如果向交换机中发送一个信息，如果rountingKey是order.release
        //此信息就会进入到order.release.queue队列中
        return new Binding(OrderConstant.ORDER_RELEASE_QUEUE,Binding.DestinationType.QUEUE,
                OrderConstant.ORDER_EXCHANGE,"order.release",null);
    }

    @Bean
    public MessageConverter messageConverter(){
        //使用json序列化机制，进行消息转换
        return new Jackson2JsonMessageConverter();
    }
}
